package com.bluefish.bltuan.action.admin;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.action.AbstractAction;
import com.bluefish.bltuan.module.user.Manager;
import com.bluefish.bltuan.service.ManagerService;
import com.bluefish.bltuan.tools.ActionUtil;
import com.bluefish.bltuan.tools.user.UserRight;

@Results({
    @Result(name = "doLogin", type = "redirect", location = "/admin/index"),
    @Result(name = "login", type = "redirect", location = "/admin/login")
})
public class LoginAction extends AbstractAction {

	private static final long serialVersionUID = 6945326916078635270L;

	private String username;
	
	private String password;
	
	private ManagerService managerService;

	private static final int NO_SUCH_USER = 100;

	private static final int ERROR_PASSWORD = 200;
	
    private static final int SUCC_VALIDATED = 300;
    
	private static final int LOGIN_SUCCESS = 400;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setManagerService(ManagerService managerService) {
		this.managerService = managerService;
	}

	@Override
	public HttpHeaders index() {
		return isLoginManager() ? new DefaultHttpHeaders("doLogin").disableCaching() : 
			new DefaultHttpHeaders(INDEX).disableCaching();
	}

	private boolean isLoginManager(){
		return (ActionUtil.getCurrentUserId() != null && ActionUtil.getCurrentUserRight() == UserRight.ADMIN);
	}
	
	@Override
	public HttpHeaders show() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public HttpHeaders create() {
		return doLogin() ? new DefaultHttpHeaders("doLogin").disableCaching() : 
			new DefaultHttpHeaders("login").disableCaching() ;
	}

	@Override
	public HttpHeaders editNew() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String update() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String edit() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String destroy() {
		return null;
	}

	public String login(){
		if(ActionUtil.getCurrentUserId() != null)
			ActionUtil.removeUserId();
		if(ActionUtil.getCurrentUserRight() != null)
			ActionUtil.removeCurrentUserRight();
		return "login";
	}
	
	private boolean doLogin(){
		Manager manager = this.managerService.doLogin(this.username, this.password);
		int message = 0;
		boolean isSuccess = false;
		if(manager != null && manager.isActivated()){
			ActionUtil.storeCurrentUserID(manager.getId());
			ActionUtil.storeCurrentUserRight(manager.getUserRight());
			message = LOGIN_SUCCESS;
			isSuccess = true;
		}else{
			message = validateUser(this.username, this.password);
		}
		addFlashMessage(message);
		return isSuccess;
	}
	
	private int validateUser(String username, String password) {
		int msg = 0;
		Manager manager = this.managerService.getByUsername(username);
		if(manager == null){
			msg = NO_SUCH_USER;
		}else{
			msg = !manager.getPassword().equals(password) ? ERROR_PASSWORD : SUCC_VALIDATED; 
		}
		
		return msg;
	}
}
